.PHONY: all clean check_obj_dir
TOPDIR =  $(shell pwd)
CPPUTEST_HOME := $(TOPDIR)/../tool/cpputest
CPPFLAGS += -I$(CPPUTEST_HOME)/include
CPPFLAGS += -Iinclude -Isrc
LD_LIBRARIES = -L $(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt -lpthread
CPPFLAGS += -MMD -MP

SOURCE:= $(wildcard src/*.cpp)
SOURCE_OBJ:= $(SOURCE:src/%.cpp=obj/src/%.o)

TESTS:= $(wildcard tests/*.cpp)
TESTS_OBJ:= $(TESTS:tests/%.cpp=obj/tests/%.o)

UT_TARGET:= obj/ut.exe

all: check_obj_dir $(UT_TARGET)
	$(UT_TARGET)

clean:
	rm -rf obj

$(UT_TARGET): $(SOURCE_OBJ) $(TESTS_OBJ)
	$(CXX) $(SOURCE_OBJ) $(TESTS_OBJ) $(LD_LIBRARIES) -o $@

$(TESTS_OBJ):obj/%.o:%.cpp
	$(CXX) $(CPPFLAGS) -c $< -o $@
-include $(TESTS_OBJ:.o=.d)
	
$(SOURCE_OBJ):obj/%.o:%.cpp
	$(CXX) $(CPPFLAGS) -c $< -o $@
-include $(SOURCE_OBJ:.o=.d)
	
check_obj_dir:
	-@mkdir -p obj obj/src obj/tests obj/it 2>/dev/null
